package day20170426;

import static org.junit.Assert.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.Test;

public class TestPostP {
	
	@Test
	public void testName() throws Exception {
		URL url = new URL("http://www.lanz.net.cn/LANZGateway/DirectSendSMSs.asp");
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
		connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");          
        
		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "GB2312");

		  String urlString = "UserID=706199&Account=13424431416&Password="
		  		+ "7A8F850E2D1FB80AB740C2402091E0DEE9868B1A&Content="
		  		+ "您的注册验证码为12353，该验证码10分钟内有效。如非本人操作请忽略此短信！【浪驰软件】&Phones=13926594604&ReturnXJ=1";
          
		urlString = urlString.replace("%","％");
		urlString = urlString.replace("+","＋");
		urlString = urlString.replace(" ","+");


		out.write(urlString);
		out.flush();
		out.close();
		
		String sCurrentLine;
		String sTotalString;
		sCurrentLine = "";
		sTotalString = "";
		InputStream l_urlStream;
		l_urlStream = connection.getInputStream();

		BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
		while ((sCurrentLine = l_reader.readLine()) != null) 
		{
			sTotalString += sCurrentLine + "\r\n";
		}
		System.out.println(sTotalString);
	}
	
	
	
	
	public void testPost() throws IOException 
	{
		URL url = new URL("http://www.lanz.net.cn/LANZGateway/DirectSendSMSs.asp");
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
		connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");          
        
		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "GB2312");

		  String urlString = "UserID=706199&Account=13424431416&Password="
		  		+ "7A8F850E2D1FB80AB740C2402091E0DEE9868B1A&Content="
		  		+ "您的注册验证码为123543，该验证码10分钟内有效。如非本人操作请忽略此短信！【浪驰软件】&Phones=13926594604&ReturnXJ=1";
          
		urlString = urlString.replace("%","％");
		urlString = urlString.replace("+","＋");
		urlString = urlString.replace(" ","+");


		out.write(urlString);
		out.flush();
		out.close();
		
		String sCurrentLine;
		String sTotalString;
		sCurrentLine = "";
		sTotalString = "";
		InputStream l_urlStream;
		l_urlStream = connection.getInputStream();

		BufferedReader l_reader = new BufferedReader(new InputStreamReader(l_urlStream));
		while ((sCurrentLine = l_reader.readLine()) != null) 
		{
			sTotalString += sCurrentLine + "\r\n";
		}
		System.out.println(sTotalString);
	}

	
}
